package com.tuanbiao.ui.plan;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.abiao.lib_utils.ViewUtils;
import com.hzjq.data.entity.ACapEntity;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.SingleCheckController;
import com.tuanbiao.databinding.ActivityDetonatorSingleCheckBinding;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.models.SingleCheckModel;
import com.tuanbiao.presenter.SingleCheckPresenter;
import com.tuanbiao.ui.plan.adapter.DetonatorSingleCheckAdapter;
import com.tuanbiao.utils.ClickUtil;
import com.tuanbiao.utils.LibCollections;

import java.util.List;

/**
 * @author abiao
 * @since 2022/4/6 17:38
 */
public class DetonatorSingleCheckActivity extends BasePresenterActivity<SingleCheckPresenter, ActivityDetonatorSingleCheckBinding> implements SingleCheckController.View {
    private DetonatorSingleCheckAdapter detonatorSingleCheckAdapter;

    public static void start(Context context) {
        Intent starter = new Intent(context, DetonatorSingleCheckActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivityDetonatorSingleCheckBinding getBindingView() {
        return ActivityDetonatorSingleCheckBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        detonatorSingleCheckAdapter = new DetonatorSingleCheckAdapter();
        detonatorSingleCheckAdapter.setOnItemLongClickListener((adapter, view, position) -> {
            ACapEntity aCapEntity = (ACapEntity) adapter.getItem(position);
            showDeleteDialog(aCapEntity);

            return false;
        });
        binding.rvContent.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvContent.setAdapter(detonatorSingleCheckAdapter);
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.srlContent.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.loadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.refresh();
            }
        });
        binding.srlContent.setEnableLoadMore(false);
        binding.srlContent.autoRefresh();
        binding.btnCommit.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.singleCheck();
        });
        binding.btnClear.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showClearDialog();
        });
    }

    private void showDeleteDialog(ACapEntity aCapEntity) {
        new MessageDialog.Builder(getActivity())
                .setMessage("确定要删除当前雷管信息吗？")
                .setListener(dialog -> mPresenter.deleteCheck(aCapEntity))
                .show();

    }

    private void showClearDialog() {
        new MessageDialog.Builder(getActivity())
                .setMessage("确定要清除所有雷管信息吗？")
                .setListener(dialog -> mPresenter.clearCheck())
                .show();
    }


    @Override
    protected SingleCheckPresenter createPresenter() {
        return new SingleCheckPresenter(new SingleCheckModel(), this);
    }

    @Override
    public void addDetonator(ACapEntity entity) {
        detonatorSingleCheckAdapter.addData(0, entity);
    }

    @Override
    public void setItems(List<ACapEntity> entities) {
        binding.srlContent.finishRefresh();
        if (LibCollections.isEmpty(entities)) {
            detonatorSingleCheckAdapter.setEmptyView(ViewUtils.layoutId2View(R.layout.include_no_data));
        }
        detonatorSingleCheckAdapter.setNewInstance(entities);
    }

    @Override
    public void addItems(List<ACapEntity> entities) {
        binding.srlContent.finishLoadMore();
        if (LibCollections.isEmpty(entities)) {
            binding.srlContent.finishLoadMoreWithNoMoreData();
        } else {
            detonatorSingleCheckAdapter.addData(entities);
        }

    }

    @Override
    public void onDetonatorDelete(ACapEntity aCapEntity) {
        detonatorSingleCheckAdapter.remove(aCapEntity);
    }

    @Override
    public void showNoteDialog(String msg) {
        new MessageDialog.Builder(getActivity())
                .setTitle(R.string.common_note)
                .setMessage(msg)
                .setAlone(true)
                .show();
    }
}
